using System;
using System.Collections.Generic;
using Csla;
using System.Data;
using ENT.PM.Core.Proyecto;
using ENT.PM.Datos.MysqlHelper;

namespace ENT.PM.Core.Tareas
{
    [Serializable]
    public class TareaLista :
      ReadOnlyListBase<TareaLista, TareaInfo>
    {
        #region Authorization Rules

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowGet(typeof(TareaList), "Role");
        }

        #endregion

        #region Factory Methods

        public static TareaLista GetTareaList(TareaCriterio criterio)
        {
            return DataPortal.Fetch<TareaLista>(criterio);
        }

        private TareaLista()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void DataPortal_Fetch(TareaCriterio criterio)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[1];
            
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("IDU", criterio.IdUsuario);
            
            foreach (DataRow items in objhm.EjecutarStoreProcedure(parametros, "Buscar_Tarea").Tables[0].Rows)
            {
                this.Add(TareaInfo.GetTareaInfo(items));
            }

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }


    [Serializable]
    public class TareaInfo : ReadOnlyBase<TareaInfo>
    {
        #region Business Methods

        public int Id { get; set; }

        public int IdRol { get; set; }

        public int IdEstado { get; set; }

        public int IdUsuario { get; set; }

        public int IdProyecto { get; set; }

        public string Proyecto { get; set; }

        public string Nombre { get; set; }

        public string Descripcion { get; set; }

        public string Fecha { get; set; }

        #endregion

        #region Factory Methods

        internal static TareaInfo GetTareaInfo(DataRow childData)
        {
            return DataPortal.FetchChild<TareaInfo>(childData);
        }

        private TareaInfo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(DataRow childData)
        {
            this.Id = Convert.ToInt32(childData[0].ToString());
            this.IdRol = Convert.ToInt32(childData[1].ToString());
            this.Nombre = childData[2].ToString();
            this.Descripcion = childData[3].ToString();            
            this.IdEstado = Convert.ToInt32(childData[4].ToString());
            this.Fecha = childData[5].ToString();
            this.IdUsuario = Convert.ToInt32(childData[6].ToString());
            this.IdProyecto =  Convert.ToInt32(childData[7].ToString());
            this.Proyecto = childData[10].ToString();
        }

        #endregion
    }
}
